package com.artron.mediaartron.ui.fragment.made.picture;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.adapter.PictureTabAdapter;
import com.artron.mediaartron.ui.callback.TitleChangeCallback;
import com.artron.mediaartron.ui.fragment.dialog.NormalDialogFragment;
import com.bilibili.boxing.AbsBoxingViewFragment;
import com.bilibili.boxing.model.entity.BaseMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.umeng.analytics.pro.x.W;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-27.
 */

public class TabFragment extends BaseStaticFragment {

    public static final String TAG = "TabFragment";
    @BindView(R.id.TabFragment_tab)
    protected TabLayout mTabLayout;
    @BindView(R.id.TabFragment_pager)
    protected ViewPager mViewPager;
    private PictureTabAdapter mAdapter;
    private boolean mIsSelected;
    private PictureSelectionFragment mBoxingFragment;

    public static TabFragment newInstance() {
        TabFragment fragment = new TabFragment();
        fragment.init();
        return fragment;
    }

    private void init() {
        mBoxingFragment = PictureSelectionFragment.newInstance(true);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_picture_selection;
    }

    @Override
    protected void initView() {
        mAdapter = new PictureTabAdapter(getChildFragmentManager(), mBoxingFragment);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                String title;
                if (position == 0) {
                    title = getString(R.string.local_album);
                } else {
                    title = getString(R.string.material_library);
                    ((PictureNetFragment) mAdapter.getFragment(1)).show();
                }
                ((TitleChangeCallback) getActivity()).setTitle(title);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_selection:
                if (mIsSelected) {
                    MediaSelectCallback fragment = (MediaSelectCallback)
                            mAdapter.getFragment(mViewPager.getCurrentItem());
                    PictureSelectionFragment boxingFragment = (PictureSelectionFragment) mAdapter.getFragment(0);

                    List<? extends BaseMedia> selectedMedias = fragment.getSelectedMedias();
                    BaseMedia imageMedia = selectedMedias.get(0);
                    float size = imageMedia.getSize() / 1024.f;
                    if (size < 500  && size != 0) {
                        NormalDialogFragment dialog = NormalDialogFragment.newInstance("提示",
                                "为保证打印质量，请选择500kb以上的图片", "", "我知道了");
                        dialog.show(getChildFragmentManager(), dialog.getOurTag());
                    } else {
                       boxingFragment.onFinish((List<BaseMedia>) selectedMedias);
                    }

                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.menu_selection);
        if (mIsSelected) {
            item.setIcon(R.drawable.ic_selection_finish_selected);
        } else {
            item.setIcon(R.drawable.ic_selection_finish_normal);
        }
        super.onPrepareOptionsMenu(menu);
    }

    public void setCheckView(boolean isSelected) {
        this.mIsSelected = isSelected;
        getActivity().invalidateOptionsMenu();
    }

    public void setSelectedBundle(ArrayList<BaseMedia> selectedBundle) {
        mBoxingFragment.setSelectedBundle(selectedBundle);
    }

    public AbsBoxingViewFragment getBoxingFragment() {
        return mBoxingFragment;
    }
}
